package cn.aylog.dao;

import cn.aylog.pojo.Employee;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> data = null;
    static {
        data = new HashMap<>();
        data.put(1001, new Employee(1001, "AA", "13811111111", 1));
        data.put(1002, new Employee(1002, "BB", "13822222222", 1));
        data.put(1003, new Employee(1003, "CC", "13833333333", 0));
        data.put(1004, new Employee(1004, "DD", "13844444444", 1));
        data.put(1005, new Employee(1005, "EE", "13855555555", 0));
    }

    private Integer index = 1006;

    /**
     * 获取所有数据
     */
    public List<Employee> listAll() {
        List<Employee> list = new ArrayList<>();
        data.values().stream().forEach(o -> list.add(o));
        return list;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Employee listById(Integer id) {
        return data.get(id);
    }

    /**
     * 更新数据
     */
    public void update(Employee emp) {
        if(data.containsKey(emp.getId())) {
            Employee temp = data.get(emp.getId());
            temp.clone(emp);
        }
    }

    /**
     * 删除数据
     */
    public Employee delete(Integer id) {
        if(!data.containsKey(id)) {
            return null;
        }
        Employee temp = data.get(id);
        data.remove(id);
        return temp;
    }

    /**
     * 新增数据
     */
    public void add(Employee emp) {
        emp.setId(this.index);
        data.put(this.index++, emp);
    }

}
